<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/03/11
 */
namespace api\modules\v1\controllers;

use common\logic\Boat;
use common\logic\Fund;
use common\logic\Occupancy;
use common\logic\Trip;
use common\logic\User;
use common\models\Booker;
use common\models\Order;
use common\models\Cabin;
use common\tool\FormField;
use yii\web\Controller;
use Yii;
use yii\web\Response;

class FundController extends Controller {
    public function beforeAction($action) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return parent::beforeAction($action);
    }

    public function actionInviteamount() {
        return [
            'code' => 200,
            'data' => [
                'amount' => Yii::$app->params['inviteFund'],
            ],
        ];
    }

    public function actionMy() {
        if (Yii::$app->user->isGuest) {
            return [
                'code' => 403,
                '请先登录'
            ];
        }
        /** @var \api\models\User $user */
        $user = Yii::$app->user->getIdentity();
        return [
            'code' => 200,
            'data' => [
                'inviteCode' => $user->getAttribute('invite_code'),
                'amount' => $user->getAttribute('fund_amount'),
            ]
        ];
    }

    public function actionList() {
        if (Yii::$app->user->isGuest) {
            return [
                'code' => 403,
                '请先登录'
            ];
        }
        $r = Fund::getFund(Yii::$app->user->id);
        if ($r) {
            foreach ($r as $k => $v) {
                $uids[] = $v['ext']['from'];
            }
            $names = User::getNames($uids);
            foreach ($r as $k => $v) {
                $r[$k]['fromUser'] = $names[$v['ext']['from']];
                unset($r[$k]['owner_uid']);
                unset($r[$k]['use_limit']);
                unset($r[$k]['use_range']);
                unset($r[$k]['etime']);
                unset($r[$k]['ext']);
            }
        }

        return [
            'code' => 200,
            'data' => [
                'list' => $r,
            ]
        ];
    }
}
